var that = this;

//获取请求URL
var host = window.location.origin;

//获取直播源JSON数据
$.getJSON(host+"/tv/show.json", function (data) {
	console.log("获取直播源JSON数据: "+data);
    that.showData(data);
});

//节目列表
var showData = function(data){
    if ('undefined' != data) {
		var ipv4Obj = data.ipv4;
		var ipv6Obj = data.ipv6;
		var backupObj = data.backup;
		
		//IPV4
		if('undefined' != ipv4Obj){
		  getIpv4List(ipv4Obj);
		}
		//IPV6
		if('undefined' != ipv6Obj){
		  getIpv6List(ipv6Obj);
		}
		//backup
		//if('undefined' != backupObj){
		  //getBackupList(backupObj);
		//}
		
		hideShowList();
    }
};


//追加IPV4内容
function getIpv4List(data) {
	//清空IPV4节目
    $("#IPV4").empty();
	
	//央视列表
	var chinaTitle = data.china;
	var chinaList = data.chinaList;
	if(chinaList.length > 0){
		var chinaTitleHtml = '<div class="episodes">'+
							 '<div class="title">'+chinaTitle+'</div>'+
							 '<div id="ipv4_china" class="box"></div>'+
							 '</div>';
		$("#IPV4").append(chinaTitleHtml);
		//升序
		var chinaSortList = sortArr(chinaList);
		for (var i = 0; i < chinaSortList.length; i++) {
			   var showDisplay = chinaSortList[i].display;
			   if('undefined' != showDisplay &&'0' == showDisplay){
				   var showName = chinaSortList[i].name;
				   var showUrl = chinaSortList[i].url;
				   var showHtml = '<div class="item" data-src="'+showUrl+'">'+showName+'</div>';
				   $("#ipv4_china").append(showHtml);
			   }
		   }
	}
	
	 //卫视列表
	var provinceTitle = data.province;
	var provinceList = data.provinceList;
	if(provinceList.length > 0){
		var provinceTitleHtml = '<div class="episodes">'+
								'<div class="title">'+provinceTitle+'</div>'+
								'<div id="ipv4_province" class="box"></div>'+
								'</div>';
		$("#IPV4").append(provinceTitleHtml);
		//升序
		var provinceSortList = sortArr(provinceList);
		for (var i = 0; i < provinceSortList.length; i++) {
			   var showDisplay = provinceSortList[i].display;
			   if('undefined' != showDisplay &&'0' == showDisplay){
				   var showName = provinceSortList[i].name;
				   var showUrl = provinceSortList[i].url;
				   var showHtml = '<div class="item" data-src="'+showUrl+'">'+showName+'</div>';
				   $("#ipv4_province").append(showHtml);
			   }
		   }
	}

	 //地方列表
	var districtTitle = data.district;
	var districtList = data.districtList;
	if(districtList.length > 0){
		var districtTitleHtml = '<div class="episodes">'+
								'<div class="title">'+districtTitle+'</div>'+
								'<div id="ipv4_district" class="box"></div>'+
								'</div>';
		$("#IPV4").append(districtTitleHtml);
		//升序
		var districtSortList = sortArr(districtList);
		for (var i = 0; i < districtSortList.length; i++) {
				var showDisplay = districtSortList[i].display;
			   if('undefined' != showDisplay &&'0' == showDisplay){
				   var showName = districtSortList[i].name;
				   var showUrl = districtSortList[i].url;
				   var showHtml = '<div class="item" data-src="'+showUrl+'">'+showName+'</div>';
				   $("#ipv4_district").append(showHtml);
			   }
		   }
	}
	
	 //其他列表
	var otherTitle = data.other;
	var otherList = data.otherList;
	if(otherList.length > 0){
		var otherTitleHtml = '<div class="episodes">'+
							 '<div class="title">'+otherTitle+'</div>'+
							 '<div id="ipv4_other" class="box"></div>'+
							 '</div>';
		$("#IPV4").append(otherTitleHtml);
		//升序
		var otherSortList = sortArr(otherList);
		for (var i = 0; i < otherSortList.length; i++) {
			   var showDisplay = otherSortList[i].display;
			   if('undefined' != showDisplay &&'0' == showDisplay){
				   var showName = otherSortList[i].name;
				   var showUrl = otherSortList[i].url;
				   var showHtml = '<div class="item" data-src="'+showUrl+'">'+showName+'</div>';
				   $("#ipv4_other").append(showHtml);
			   }
		   }
	}
}

//追加IPV6内容
function getIpv6List(data) {
	//清空IPV6节目
    $("#IPV6").empty();
	
	//央视列表
	var chinaTitle = data.china;
	var chinaList = data.chinaList;
	if(chinaList.length > 0){
		var chinaTitleHtml = '<div class="episodes">'+
							 '<div class="title">'+chinaTitle+'</div>'+
							 '<div id="ipv6_china" class="box"></div>'+
							 '</div>';
		$("#IPV6").append(chinaTitleHtml);
		//升序
		var chinaSortList = sortArr(chinaList);
		for (var i = 0; i < chinaSortList.length; i++) {
			   var showDisplay = chinaSortList[i].display;
			   if('undefined' != showDisplay &&'0' == showDisplay){
				   var showName = chinaSortList[i].name;
				   var showUrl = chinaSortList[i].url;
				   var showHtml = '<div class="item" data-src="'+showUrl+'">'+showName+'</div>';
				   $("#ipv6_china").append(showHtml);
			   }
		   }
	}
	
	 //卫视列表
	var provinceTitle = data.province;
	var provinceList = data.provinceList;
	if(provinceList.length > 0){
		var provinceTitleHtml = '<div class="episodes">'+
								'<div class="title">'+provinceTitle+'</div>'+
								'<div id="ipv6_province" class="box"></div>'+
								'</div>';
		$("#IPV6").append(provinceTitleHtml);
		//升序
		var provinceSortList = sortArr(provinceList);
		for (var i = 0; i < provinceSortList.length; i++) {
			   var showDisplay = provinceSortList[i].display;
			   if('undefined' != showDisplay &&'0' == showDisplay){
				   var showName = provinceSortList[i].name;
				   var showUrl = provinceSortList[i].url;
				   var showHtml = '<div class="item" data-src="'+showUrl+'">'+showName+'</div>';
				   $("#ipv6_province").append(showHtml);
			   }
		   }
	}

	 //地方列表
	var districtTitle = data.district;
	var districtList = data.otherList;
	if(districtList.length > 0){
		var districtTitleHtml = '<div class="episodes">'+
								'<div class="title">'+districtTitle+'</div>'+
								'<div id="ipv6_district" class="box"></div>'+
								'</div>';
		$("#IPV6").append(districtTitleHtml);
		//升序
		var districtSortList = sortArr(districtList);
		for (var i = 0; i < districtSortList.length; i++) {
			   var showDisplay = districtSortList[i].display;
			   if('undefined' != showDisplay &&'0' == showDisplay){
				   var showName = districtSortList[i].name;
				   var showUrl = districtSortList[i].url;
				   var showHtml = '<div class="item" data-src="'+showUrl+'">'+showName+'</div>';
				   $("#ipv6_district").append(showHtml);
			   }
		   }
	}
	
	 //其他列表
	var otherTitle = data.other;
	var otherList = data.otherList;
	if(otherList.length > 0){
		var otherTitleHtml = '<div class="episodes">'+
							 '<div class="title">'+otherTitle+'</div>'+
							 '<div id="ipv6_other" class="box"></div>'+
							 '</div>';
		$("#IPV6").append(otherTitleHtml);
		//升序
		var otherSortList = sortArr(otherList);
		for (var i = 0; i < otherSortList.length; i++) {
			   var showDisplay = otherSortList[i].display;
			   if('undefined' != showDisplay &&'0' == showDisplay){
				   var showName = otherSortList[i].name;
				   var showUrl = otherSortList[i].url;
				   var showHtml = '<div class="item" data-src="'+showUrl+'">'+showName+'</div>';
				   $("#ipv6_other").append(showHtml);
			   }
		   }
	}
}

//追加备份内容
function getBackupList(data) {
	
	//清空备份节目
    $("#IPV4_BACK").empty();
	
	//备份IPV4
	var ipv4List = data.ipv4List;
	if(ipv4List.length > 0){
		var ipv4BackupTitleHtml = '<div class="episodes">'+
							 '<div class="title" onclick="displayBackup()">IPV4列表</div>'+
							 '<div id="ipv4_backup_list" style="display:none" class="box"></div>'+
							 '</div>';
		$("#IPV4_BACK").append(ipv4BackupTitleHtml);
	   //升序
	   var ipv4BackupSortList = sortArr(ipv4List);
	   for (var i = 0; i < ipv4BackupSortList.length; i++) {	
		   var showDisplay = ipv4BackupSortList[i].display;
		   if('undefined' != showDisplay &&'0' == showDisplay){	   
				var showName = ipv4BackupSortList[i].name;
				var showUrl = ipv4BackupSortList[i].url;
				var showHtml = '<div class="item" data-src="'+showUrl+'">'+showName+'</div>';
				$("#ipv4_backup_list").append(showHtml);
		   }
		}
	}
	
	//清空备份节目
    $("#IPV6_BACK").empty();
	
	//备份IPV6
	var ipv6List = data.ipv6List;
	if(ipv6List.length > 0){
		var ipv6BackupTitleHtml = '<div onclick="displayBackup()" class="episodes">'+
							 '<div class="title">IPV6列表</div>'+
							 '<div id="ipv6_backup_list" style="display:none" class="box"></div>'+
							 '</div>';
		$("#IPV6_BACK").append(ipv6BackupTitleHtml);
	   //升序
	   var ipv4BackupSortList = sortArr(ipv6List);
	   for (var i = 0; i < ipv4BackupSortList.length; i++) {	
		   var showDisplay = ipv4BackupSortList[i].display;
		   if('undefined' != showDisplay &&'0' == showDisplay){	   
				var showName = ipv4BackupSortList[i].name;
				var showUrl = ipv4BackupSortList[i].url;
				var showHtml = '<div class="item" data-src="'+showUrl+'">'+showName+'</div>';
				$("#ipv6_backup_list").append(showHtml);
		   }
		}
	}
}

//隐藏节目列表
function hideShowList() {
	if(IsPC()){
		//隐藏IPV6节目
		var IPV6Show = document.getElementById('IPV6');
		IPV6Show.style.display = 'none';
		
		//var IPV6BackupShow = document.getElementById('IPV6_BACK');
		//IPV6BackupShow.style.display = 'none';
	}else{	
		//是否支持IPV6
		$.getJSON("https://test.ipw.cn/api/ip/myip?json", function (data) {
			if(data.result){
			console.log("是否支持IPV6: "+data.IPVersion);
			   if(data.IPVersion == "IPv4"){
				//隐藏IPV6节目
				var IPV4Show = document.getElementById('IPV6');
				IPV4Show.style.display = 'none';
				
				//var IPV6BackupShow = document.getElementById('IPV6_BACK');
				//IPV6BackupShow.style.display = 'none';
			   }else{
				//隐藏IPV4节目
				var IPV6Show = document.getElementById('IPV4');
				IPV6Show.style.display = 'none';
				
				//var IPV4BackupShow = document.getElementById('IPV4_BACK');
				//IPV4BackupShow.style.display = 'none';
			   }
			}
		});
	}
}
	
//显示备份节目列表
function displayBackup(){
	if(IsPC()){
		debugger;
		//显示IPV4节目
		var IPV4BackupShow = document.getElementById('ipv6_backup_list');
		IPV4BackupShow.style.display = 'block';
	}else{
		//显示IPV6节目
		var IPV6BackupShow = document.getElementById('ipv6_backup_list');
		IPV6BackupShow.style.display = 'block';
	}
}

	
//冒泡法排序:从小到大
function sortArr(arr, attr ='sort',flag=true) {
	for (var i = 0; i < arr.length - 1; i++) {//比较arr.length-1轮
		for (var j = i + 1; j < arr.length; j++) {
			if (arr[i][attr] > arr[j][attr] && flag == true) {//交换
				var temp = arr[i]//临时变量
				arr[i] = arr[j];
				arr[j] = temp;

			}else if(arr[i][attr] < arr[j][attr] && flag == false){
				var temp = arr[i];//临时变量
				arr[i] = arr[j];
				arr[j] = temp;
			}
		}
	}
	return arr;
}